package com.andyliu.sys.controller;

import com.andyliu.sys.pojo.SDept;
import com.andyliu.sys.service.DeptService;
import com.andyliu.vo.ParamVO;
import com.andyliu.vo.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: DeptController
 * Description: TODO
 * date: 2021/1/20 5:20 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@RestController
@RequestMapping("sys/dept")
@Api(tags = "部门请求",protocols = "http",produces = "application/json")
public class DeptController {
    @Autowired
    private DeptService  deptService;

    @PostMapping("save")
    @ApiOperation(value = "保存部门",
            notes = "编辑，新增部门信息",
            httpMethod = "POST")
    public Result saveDept(@ApiParam(name = "请求部门的相关信息",
            required = true,
            value = "属性序列化后的值")
                                       @RequestBody SDept dept) {
        Boolean status = deptService.saveDept(dept);
        Result result = Result.ok(status);
        return result;
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询部门信息",
            notes = "根据id查询部门信息",
            httpMethod = "GET")
    public Result<Object> selectUser(@ApiParam(name = "id", value = "120", required = true) @PathVariable Integer id) {
        SDept dept = deptService.selectDeptById(id);
        Result<Object> rs = Result.ok("",dept);
        return rs;
    }

    @GetMapping("/parentDept")
    @ApiOperation(value = "查询一级部门信息",
            notes = "根据所有一级部门信息",
            httpMethod = "GET")
    public Result<Object> selectParents() {
        List<SDept> dept = deptService.queryDeptParent();
        Result<Object> rs = Result.ok("",dept);
        return rs;
    }

    @PostMapping("query")
    @ApiOperation(notes = "部门查询",
            value = "部门分页查询",
            httpMethod = "GET")
    public Result<Object> selectDepts(
            @ApiParam(value = "输入参数")
            @RequestBody ParamVO<SDept> dept) {
        PageInfo<SDept> page = deptService.selectDeptByPage(dept.getParam(), dept.getPageNumber(), dept.getPageSize());
        Result<Object> rs = Result.ok("",page);
        return rs;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(notes = "根据id删除部门信息",
            value = "根据id删除部门信息",
            httpMethod = "DELETE")
    public Result<Object> deleteDept(
            @ApiParam(name = "id", value = "需要删除的id", defaultValue = "688", required = true)
            @RequestParam(required = true) Integer id) {
        Boolean status = deptService.deleteDeptById(id);
        Result<Object> rs = Result.ok("",status);
        return rs;
    }

    @DeleteMapping("/deletes")
    @ApiOperation(notes = "批量删除",
            value = "批量删除",
            httpMethod = "DELETE")
    public Result<Object> deleteDepts(
            @ApiParam(name = "ids", value = "需要批量删除的id数组") Integer[] ids) {
        Boolean status = deptService.deleteDeptById(ids);
        Result<Object> rs = Result.ok(status);
        return rs;
    }

    @PostMapping("/setUserEnabled")
    @ApiOperation(value = "设置部门启用和停用",
            notes = "设置用户部门停用",
            httpMethod = "POST")
    public Result<Object> setUserEnabled(
            @ApiParam(name = "id", value = "需要启用｜停用的id")
            @RequestParam(required = true) Integer id,
            @ApiParam(name = "status", value = "需要启用或停用")
            @RequestParam(required = true) Integer status) {
        SDept dept = new SDept();
        dept.setId(id);
        dept.setStatus(status);
        Result<Object> rs = Result.ok(dept);
        return rs;
    }
    @GetMapping("/querys")
    @ApiOperation(value = "查询所有部门信息")
    public Result<Object> query(){
        List<SDept> sDepts = deptService.queryDepts();
        return Result.ok("",sDepts);
    }
}
